package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.product.domain.InsReagentBox;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.product.domain.InsReagentBoxBac;
import com.ruoyi.project.product.service.IInsReagentBoxBacService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 试剂盒记录Controller
 *
 * @author taney
 * @date 2020-12-30
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/reagentBoxBac")
public class InsReagentBoxBacController extends BaseController {

    private final IInsReagentBoxBacService iInsReagentBoxBacService;

    /**
     * 查询试剂盒记录列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBoxBac:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagentBoxBac insReagentBoxBac) {
        startPage();
        LambdaQueryWrapper<InsReagentBoxBac> lqw = new LambdaQueryWrapper<InsReagentBoxBac>();
        if (insReagentBoxBac.getOrderListId() != null) {
            lqw.eq(InsReagentBoxBac::getOrderListId, insReagentBoxBac.getOrderListId());
        }
        if (insReagentBoxBac.getOrderId() != null) {
            lqw.eq(InsReagentBoxBac::getOrderId, insReagentBoxBac.getOrderId());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getMachineType())) {
            lqw.eq(InsReagentBoxBac::getMachineType, insReagentBoxBac.getMachineType());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getProjectCode())) {
            lqw.eq(InsReagentBoxBac::getProjectCode, insReagentBoxBac.getProjectCode());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getProjectName())) {
            lqw.like(InsReagentBoxBac::getProjectName, insReagentBoxBac.getProjectName());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getTestMethod())) {
            lqw.eq(InsReagentBoxBac::getTestMethod, insReagentBoxBac.getTestMethod());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getItemNo())) {
            lqw.eq(InsReagentBoxBac::getItemNo, insReagentBoxBac.getItemNo());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getReagentSpec())) {
            lqw.eq(InsReagentBoxBac::getReagentSpec, insReagentBoxBac.getReagentSpec());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getExpireDate())) {
            lqw.eq(InsReagentBoxBac::getExpireDate, insReagentBoxBac.getExpireDate());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getLotNo())) {
            lqw.eq(InsReagentBoxBac::getLotNo, insReagentBoxBac.getLotNo());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getBarcode())) {
            lqw.eq(InsReagentBoxBac::getBarcode, insReagentBoxBac.getBarcode());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getRegistrationNo())) {
            lqw.eq(InsReagentBoxBac::getRegistrationNo, insReagentBoxBac.getRegistrationNo());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getProductionLicenseNo())) {
            lqw.eq(InsReagentBoxBac::getProductionLicenseNo, insReagentBoxBac.getProductionLicenseNo());
        }
        if (StringUtils.isNotBlank(insReagentBoxBac.getPrintState())) {
            lqw.eq(InsReagentBoxBac::getPrintState, insReagentBoxBac.getPrintState());
        }
        lqw.orderByDesc(InsReagentBoxBac::getOrderId);

        List<InsReagentBoxBac> list = iInsReagentBoxBacService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出试剂盒记录列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBoxBac:export')")
    @Log(title = "试剂盒记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsReagentBoxBac insReagentBoxBac) {
        LambdaQueryWrapper<InsReagentBoxBac> lqw = new LambdaQueryWrapper<InsReagentBoxBac>(insReagentBoxBac);
        List<InsReagentBoxBac> list = iInsReagentBoxBacService.list(lqw);
        ExcelUtil<InsReagentBoxBac> util = new ExcelUtil<InsReagentBoxBac>(InsReagentBoxBac.class);
        return util.exportExcel(list, "reagentBoxBac");
    }

    /**
     * 获取试剂盒记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBoxBac:query')")
    @GetMapping(value = "/{boxId}")
    public AjaxResult getInfo(@PathVariable("boxId") Integer boxId) {
        return AjaxResult.success(iInsReagentBoxBacService.getById(boxId));
    }

    /**
     * 新增试剂盒记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBoxBac:add')")
    @Log(title = "试剂盒记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagentBoxBac insReagentBoxBac) {
        return toAjax(iInsReagentBoxBacService.save(insReagentBoxBac) ? 1 : 0);
    }

    /**
     * 修改试剂盒记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBoxBac:edit')")
    @Log(title = "试剂盒记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagentBoxBac insReagentBoxBac) {
        return toAjax(iInsReagentBoxBacService.updateById(insReagentBoxBac) ? 1 : 0);
    }

    /**
     * 删除试剂盒记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBoxBac:remove')")
    @Log(title = "试剂盒记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{boxIds}")
    public AjaxResult remove(@PathVariable Integer[] boxIds) {
        return toAjax(iInsReagentBoxBacService.removeByIds(Arrays.asList(boxIds)) ? 1 : 0);
    }

    /**
     * 获取某个订单的第一个盒贴条码
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:getOrderFirstBarcode')")
    @GetMapping("/getOrderFirstBarcode/{orderId}")
    public AjaxResult getOrderFirstBarcode(@PathVariable Integer orderId) {
        LambdaQueryWrapper<InsReagentBoxBac> lqw = new LambdaQueryWrapper<>();
        lqw.eq(InsReagentBoxBac::getOrderId, orderId);
        return AjaxResult.success(iInsReagentBoxBacService.getMap(lqw));
    }
}